function [ output_args ] = srtgen( labels, combined_labels, speed_vector, time_offset, fname )
%SRTGEN Summary of this function goes here
%   Detailed explanation goes here

f = fopen(fname, 'w');
srtid = 0;
for i=1:size(combined_labels, 2)
    start_time = combined_labels(1, i) + time_offset;
    end_time = combined_labels(2, i) + time_offset;
    if start_time < 0
        start_time = 0;
    end
    if end_time < 0
        continue;
    end
    if isempty(speed_vector{i})
        start_time = get_strtime(start_time);
        end_time = get_strtime(end_time);
        srtid = srtid+1;
        text = labels{combined_labels(3, i)}.class;
        fprintf(f, '%d\n%s --> %s\n%s\n\n', srtid, start_time, end_time, text);
    else
        for j=1:size(speed_vector{i}, 1)-1
            start_time1 = get_strtime(speed_vector{i}(j, 1) + time_offset);
            end_time1 = get_strtime(speed_vector{i}(j+1, 1) + time_offset);
            srtid = srtid+1;
            text = [labels{combined_labels(3, i)}.class ' @ ' num2str(speed_vector{i}(j, 2)) ' steps/s'];
            fprintf(f, '%d\n%s --> %s\n%s\n\n', srtid, start_time1, end_time1, text);            
        end
        if speed_vector{i}(end, 1) + time_offset < end_time
            start_time1 = get_strtime(speed_vector{i}(end, 1) + time_offset);
            end_time1 = get_strtime(end_time);
            srtid = srtid+1;
            text = [labels{combined_labels(3, i)}.class ' @ ' num2str(speed_vector{i}(end, 2)) ' steps/s'];
            fprintf(f, '%d\n%s --> %s\n%s\n\n', srtid, start_time1, end_time1, text);
        end
    end
end
fclose(f);

end

function str_time = get_strtime( t )
    m = floor(t / 60);
    t = t - m * 60;
    s = floor(t);
    t = round(1000 * (t - s));
    h = floor(m / 60);
    m = m - h * 60;
    str_time = sprintf('%02d:%02d:%02d,%03d', h, m, s, t);
end
